Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing coefficient type in read_from_file #3801

Merged
merged 6 commits into from
Aug 8, 2024
Merged

Conversation

blegat
Copy link
Member

@blegat blegat commented Aug 7, 2024

Before this PR

julia> file = joinpath(dirname(dirname(pathof(MOI))), "test", "FileFormats", "SDPA", "models", "example_A.dat-s")
"/home/blegat/.julia/packages/MathOptInterface/jqDoD/test/FileFormats/SDPA/models/example_A.dat-s"

julia> model = read_from_file(file)
A JuMP Model
Minimization problem with:
Variables: 2
Objective function type: AffExpr
`Vector{AffExpr}`-in-`MathOptInterface.PositiveSemidefiniteConeTriangle`: 2 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> set_optimizer(model, Clarabel.Optimizer{BigFloat})

julia> optimize!(model)
ERROR: MathOptInterface.UnsupportedAttribute{MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarAffineFunction{Float64}}}: Attribute MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarAffineFunction{Float64}}() is not supported by the model.
Stacktrace:
  [1] bridge_type(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MathOptInterface.Utilities.CachingOptimizer{…}}, F::Type{MathOptInterface.ScalarAffineFunction{…}})
    @ MathOptInterface.Bridges ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/lazy_bridge_optimizer.jl:546
  [2] concrete_bridge_type(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MathOptInterface.Utilities.CachingOptimizer{…}}, F::Type{MathOptInterface.ScalarAffineFunction{…}})
    @ MathOptInterface.Bridges.Objective ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/Objective/bridge.jl:68
  [3] set(b::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, attr::MathOptInterface.ObjectiveFunction{…}, func::MathOptInterface.ScalarAffineFunction{…})
    @ MathOptInterface.Bridges ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/bridge_optimizer.jl:1174
  [4] _pass_attribute(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…}, index_map::MathOptInterface.Utilities.IndexMap, attr::MathOptInterface.ObjectiveFunction{…})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/copy.jl:51
  [5] pass_attributes(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…}, index_map::MathOptInterface.Utilities.IndexMap)
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/copy.jl:38
  [6] default_copy_to(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/copy.jl:391
  [7] copy_to
    @ ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/bridge_optimizer.jl:442 [inlined]
  [8] optimize!
    @ ~/.julia/packages/MathOptInterface/jqDoD/src/MathOptInterface.jl:121 [inlined]
  [9] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{…}, MathOptInterface.Utilities.UniversalFallback{…}})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/cachingoptimizer.jl:321
 [10] optimize!(model::Model; ignore_optimize_hook::Bool, _differentiation_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::@Kwargs{})
    @ JuMP ~/.julia/packages/JuMP/7rBNn/src/optimizer_interface.jl:595
 [11] optimize!(model::Model)
    @ JuMP ~/.julia/packages/JuMP/7rBNn/src/optimizer_interface.jl:546
 [12] top-level scope
    @ REPL[34]:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> add_bridge(model, MOI.Bridges.Constraint.NumberConversionBridge, coefficient_type=BigFloat)

julia> optimize!(model)
ERROR: MethodError: no method matching promote_operation(::typeof(-), ::Type{Float64}, ::Type{MathOptInterface.VectorQuadraticFunction{BigFloat}})

Closest candidates are:
  promote_operation(::typeof(-), ::Type{T}, ::Type{MathOptInterface.VectorNonlinearFunction}) where T<:Number
   @ MathOptInterface ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/promote_operation.jl:177
  promote_operation(::typeof(-), ::Type{T}, ::Type{MathOptInterface.VectorOfVariables}) where T<:Number
   @ MathOptInterface ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/promote_operation.jl:169
  promote_operation(::typeof(-), ::Type{T}, ::Type{F}) where {T, F<:Union{AbstractVector{T}, MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarNonlinearFunction, MathOptInterface.ScalarQuadraticFunction{T}, MathOptInterface.VectorAffineFunction{T}, MathOptInterface.VectorQuadraticFunction{T}, T}}
   @ MathOptInterface ~/.julia/packages/MathOptInterface/jqDoD/src/Utilities/promote_operation.jl:142
  ...

Stacktrace:
   [1] concrete_bridge_type(::Type{MathOptInterface.Bridges.Constraint.NonnegToNonposBridge{…}}, G::Type{MathOptInterface.VectorQuadraticFunction{…}}, ::Type{MathOptInterface.Nonnegatives})
     @ MathOptInterface.Bridges.Constraint ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/Constraint/bridges/flip_sign.jl:238
   [2] node(b::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, F::Type{…}, S::Type{…})
     @ MathOptInterface.Bridges ~/.julia/packages/MathOptInterface/jqDoD/src/Bridges/lazy_bridge_optimizer.jl:327

After this PR:

julia> model = read_from_file(file, coefficient_type = BigFloat)
A JuMP Model
Minimization problem with:
Variables: 2
Objective function type: GenericAffExpr{BigFloat, GenericVariableRef{BigFloat}}
`Vector{GenericAffExpr{BigFloat, GenericVariableRef{BigFloat}}}`-in-`MathOptInterface.PositiveSemidefiniteConeTriangle`: 2 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> set_optimizer(model, Clarabel.Optimizer{BigFloat})

julia> optimize!(model)
-------------------------------------------------------------
           Clarabel.jl v0.9.0  -  Clever Acronym              
                   (c) Paul Goulart                          
                University of Oxford, 2022                   
-------------------------------------------------------------

problem:
  variables     = 2
  constraints   = 6
  nnz(P)        = 0
  nnz(A)        = 6
  cones (total) = 2
    : PSDTriangle = 2,  numel = (3,3)

settings:
  linear algebra: direct / qdldl, precision: BigFloat (256 bit)
  max iter = 200, time limit = Inf,  max step = 0.990
  tol_feas = 1.0e-08, tol_gap_abs = 1.0e-08, tol_gap_rel = 1.0e-08,
  static reg : on, ϵ1 = 1.0e-08, ϵ2 = 4.9e-32
  dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-07
  iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12, 
               max iter = 10, stop ratio = 5.0
  equilibrate: on, min_scale = 1.0e-04, max_scale = 1.0e+04
               max iter = 10

iter    pcost        dcost       gap       pres      dres      k/t        μ       step      
---------------------------------------------------------------------------------------------
  0   2.3612e+01   2.3612e+01  0.00e+00  4.12e-01  1.27e-17  1.00e+00  5.48e+00   ------   
  1   2.5231e+01   2.5542e+01  1.23e-02  1.86e-01  4.82e-18  6.99e-01  2.02e+00  6.89e-01  
  2   2.8848e+01   2.9441e+01  2.05e-02  7.56e-02  2.56e-17  7.98e-01  9.21e-01  7.76e-01  
  3   2.9576e+01   2.9650e+01  2.52e-03  1.01e-02  3.62e-18  1.01e-01  1.20e-01  8.73e-01  
  4   2.9873e+01   2.9928e+01  1.84e-03  5.53e-03  1.14e-18  6.98e-02  6.72e-02  6.27e-01  
  5   2.9949e+01   2.9960e+01  3.56e-04  1.08e-03  9.18e-19  1.35e-02  1.32e-02  8.13e-01  
  6   2.9985e+01   2.9991e+01  2.09e-04  5.22e-04  1.58e-18  7.68e-03  6.33e-03  6.96e-01  
  7   2.9994e+01   2.9995e+01  4.97e-05  1.25e-04  8.36e-19  1.83e-03  1.54e-03  7.78e-01  
  8   2.9999e+01   2.9999e+01  1.88e-05  4.27e-05  4.24e-19  6.77e-04  5.25e-04  7.92e-01  
  9   2.9999e+01   3.0000e+01  4.65e-06  1.07e-05  1.06e-19  1.68e-04  1.33e-04  7.78e-01  
 10   3.0000e+01   3.0000e+01  1.12e-06  2.40e-06  2.44e-20  4.01e-05  2.97e-05  8.78e-01  
 11   3.0000e+01   3.0000e+01  2.47e-07  5.36e-07  3.43e-21  8.84e-06  6.74e-06  8.00e-01  
 12   3.0000e+01   3.0000e+01  4.12e-08  8.51e-08  9.09e-21  1.46e-06  1.07e-06  9.25e-01  
 13   3.0000e+01   3.0000e+01  7.59e-09  1.60e-08  1.02e-21  2.71e-07  2.03e-07  8.25e-01  
 14   3.0000e+01   3.0000e+01  1.12e-09  2.31e-09  1.91e-19  3.99e-08  2.91e-08  9.38e-01  
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time = 35.2ms

julia> value.(all_variables(model))
2-element Vector{BigFloat}:
 0.9999999974529094769438466978694298260572236430943294845572438659429688710785798
 0.9999999978004611800133183212387879815915688604520797152607053647751640272731174

I'm a bit hesitant between calling this keyword argument coefficient_type (like JuMP.add_bridge) or value_type. For add_bridge, IIRC, we chose coefficient_type because it didn't really have to match with the model value type. In jump-dev/MathOptInterface.jl#2532, value_type as a keyword could be weird because value is out of scope there and we could say that in this PR we use coefficient_type to be consistent with MOI.FileFormats.Model.

Requires

@odow
Copy link
Member

odow commented Aug 8, 2024

This is also going to require a bump in the minimum MOI compat bound

@odow
Copy link
Member

odow commented Aug 8, 2024

So I realized we already support this:

julia> using JuMP

julia> file = joinpath(dirname(dirname(pathof(MOI))), "test", "FileFormats", "SDPA", "models", "example_A.dat-s")
"/Users/oscar/.julia/packages/MathOptInterface/2CULs/test/FileFormats/SDPA/models/example_A.dat-s"

julia> model = open(file, "r") do io
           return read(
               io,
               GenericModel{BigFloat};
               format = MOI.FileFormats.FORMAT_SDPA,
               number_type = BigFloat,
           )
       end
A JuMP Model
Minimization problem with:
Variables: 2
Objective function type: GenericAffExpr{BigFloat, GenericVariableRef{BigFloat}}
`Vector{GenericAffExpr{BigFloat, GenericVariableRef{BigFloat}}}`-in-`MathOptInterface.PositiveSemidefiniteConeTriangle`: 2 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

Perhaps it just needs documenting.

Reading non-Float64 file formats is pretty niche.

@odow
Copy link
Member

odow commented Aug 8, 2024

I don't think we should do this. As yet, most file formats do NOT support reading coefficients other than Float64.

@blegat
Copy link
Member Author

blegat commented Aug 8, 2024

Oh indeed, I forgot about that keyword argument of SDPA. This PR provides a simpler solution now that simply resolves your TODO 😇

@odow
Copy link
Member

odow commented Aug 8, 2024

It's not sufficient just to create a Model{T} object.

@odow
Copy link
Member

odow commented Aug 8, 2024

Oh, you've removed the kwarg here.

@blegat
Copy link
Member Author

blegat commented Aug 8, 2024

It's not sufficient just to create a Model{T} object.

I assume that if a file format allow specifying the type with a keyword argument then it will have been tested for different types as well.

src/file_formats.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member

odow commented Aug 8, 2024

I assume that if a file format allow specifying the type with a keyword argument

Yeah yeah I thought we were still having the two separate kwargs. One at the MOI level and one at the JuMP level. Disregard.

Copy link
Member

@odow odow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. But it needs a test.

Copy link

codecov bot commented Aug 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.40%. Comparing base (886a652) to head (c6a5688).
Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3801      +/-   ##
==========================================
+ Coverage   98.38%   98.40%   +0.02%     
==========================================
  Files          44       44              
  Lines        5879     5968      +89     
==========================================
+ Hits         5784     5873      +89     
  Misses         95       95              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@blegat blegat merged commit 23ccb2c into master Aug 8, 2024
11 checks passed
@blegat blegat deleted the bl/file_format_T branch August 8, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants